package org.zero.onlinesubject.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.zero.onlinesubject.common.constant.RedisConstant;
import org.zero.onlinesubject.entity.TaskDetails;
import org.zero.onlinesubject.mapper.TaskDetailsMapper;
import org.zero.onlinesubject.service.TaskDetailService;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class TaskDetailServiceImpl extends ServiceImpl<TaskDetailsMapper, TaskDetails>
        implements TaskDetailService {

    @Resource
    private TaskDetailsMapper taskDetailsMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public TaskDetails getTaskDetail(String taskId) {
        TaskDetails taskDetails = null;
        String json = stringRedisTemplate.opsForValue().get(RedisConstant.TASK_DETAILS + taskId);
        if(!StringUtils.isNullOrEmpty(json)){
            taskDetails = JSON.parseObject(json, TaskDetails.class);
            return taskDetails;
        }
        QueryWrapper<TaskDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("PubID",taskId);
        taskDetails = taskDetailsMapper.selectOne(wrapper);
        stringRedisTemplate.opsForValue().set(RedisConstant.TASK_DETAILS+taskId,JSON.toJSONString(taskDetails),30, TimeUnit.MINUTES);
        return taskDetails;
    }
}
